import { Component } from '@angular/core';
import { NbAuthResult, NbRegisterComponent } from '@nebular/auth';
import { v4 as uuidv4 } from 'uuid';
import { UserDto } from '@nx-chat/common/dto';

@Component({
  selector: 'chat-register',
  templateUrl: './register.component.html'
})
export class RegisterComponent extends NbRegisterComponent {

  public user: UserDto;

  isAgree: boolean = false;

  async register() {
    this.errors = [];
    this.messages = [];
    this.user.uid = await uuidv4();
    this.service.register('email', this.user)
      .subscribe((authResult: NbAuthResult) => {
        console.log(authResult);
        const res = authResult.getResponse();
        if (res.body.success) {
          this.messages = authResult.getMessages();
          setTimeout(() => {
            this.router.navigate(['/auth']);
          }, 1000);
        } else {
          this.showMessages.error = true;
          this.errors = res.body.errors;
        }
      })
  }

}
